package com.nova.android.tvlauncher.ui.fragment

import android.content.pm.ApplicationInfo
import android.os.Bundle
import android.view.ViewGroup
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.AppCacheX88
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.APPInfo
import com.nova.android.tvlauncher.bean.Data
import com.nova.android.tvlauncher.bean.FavoriteAppDto
import com.nova.android.tvlauncher.bean.SubData
import com.nova.android.tvlauncher.databinding.FragmentHomeBinding
import com.nova.android.tvlauncher.databinding.ItemHomeFavoriteChildBinding
import com.nova.android.tvlauncher.databinding.ItemHomeFavoriteParentBinding
import com.nova.android.tvlauncher.databinding.ItemHomeLanChildBinding
import com.nova.android.tvlauncher.databinding.ItemHomeParentBinding
import com.nova.android.tvlauncher.databinding.ItemHomePorChildBinding
import com.nova.android.tvlauncher.ui.view.ImageCarouselView
import com_github_lib_base.view.rv.addModels
import com_github_lib_base.view.rv.multiTypeAdapter
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.dimenValue
import com_github_lib_base.ext.getDrawableByName
import com_github_lib_base.ext.height
import com_github_lib_base.ext.stringValue

class HomeFragment : BaseVMFragment<FragmentHomeBinding, BaseViewModel>() {

    override fun initView() {
        val homeInfo = AppCacheX88.homeInfo

        mBind.banner.apply {
            setImages(
                listOf(
                    R.drawable.video_cover_big_2,
                )
            )
            startAutoScroll()
        }
        mBind.tvTag.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                mBind.tvTag.text = "观看 NetFlix"
                mBind.rlTag.height(com.codeZeng.lib_dimen.R.dimen.qb_px_150.dimenValue())
            } else {
                mBind.tvTag.text = ""
                mBind.rlTag.height(ViewGroup.LayoutParams.WRAP_CONTENT)
            }
        }

        mBind.rvHome.multiTypeAdapter<Any> {
            registerType {
                match<ItemHomeParentBinding>(
                    predicate = { it is SubData },
                ) { item, position ->
                    val dtoParent = item as SubData
                    rvContent.multiTypeAdapter<Any> {
                        registerType {
                            match<ItemHomePorChildBinding>(
                                predicate = { it is Data && dtoParent.type == 0 },
                            ) { item, position ->
                                val dtoChild = item as Data
                                if (dtoChild.isLocal) {
                                    this.ivContent.setImageDrawable(dtoChild.imageUrl?.getDrawableByName())
                                }
                            }
                        }

                        registerType {
                            match<ItemHomeLanChildBinding>(
                                predicate = { it is Data },
                            ) { item, position ->
                                val dtoChild = item as Data
                                if (dtoChild.isLocal) {
                                    this.ivContent.setImageDrawable(dtoChild.imageUrl?.getDrawableByName())
                                }
                            }
                        }

                    }
                    dtoParent.datas?.let {
                        it.let {
                            rvContent.addModels(it)
                        }
                    }

                }
            }

            registerType {
                match<ItemHomeFavoriteParentBinding>(
                    predicate = { it is FavoriteAppDto }
                ) { item, position ->
                    val dtoParent = item as FavoriteAppDto
                    rvFavoriteApps.multiTypeAdapter {
                        registerType {
                            match<ItemHomeFavoriteChildBinding>(
                                predicate = { true }
                            ) { item: APPInfo, position: Int ->
                                //if(item.favoriteApp.packageName==null){
                                ivIconSmall.setImageResource(R.drawable.icon_add)
                                //}
                            }
                        }
                    }
                    rvFavoriteApps.addModels(dtoParent.appInfoList)
                }
            }
        }
        val list = mutableListOf<Any>()

        homeInfo?.movies?.get(0)?.subData?.let { list.addAll(it) }


        val appInfoList = mutableListOf<APPInfo>()
        appInfoList.add(APPInfo(ApplicationInfo()))

        list.addAll(
            listOf(
                FavoriteAppDto(
                    com.nova.lib_lang.R.string.favourite_apps.stringValue(),
                    appInfoList
                )
            )
        )

        mBind.rvHome.addModels(list)


    }

    companion object {
        @JvmStatic
        fun newInstance() =
            HomeFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }

}